package com.gqz.equip.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.defect.api.EnDefectsSheetApi;
import com.gqz.defect.api.EnStockEquipDefectApi;
import com.gqz.equip.app.entity.*;
import com.gqz.equip.app.mapper.EnStockTaskMapper;
import com.gqz.equip.app.mapper.EnWzKfglMapper;
import com.gqz.equip.app.mapper.EquipEnWzJfEquipMapper;
import com.gqz.equip.app.mapper.TEnWzEquipMapper;
import com.gqz.equip.entity.EnWzJfglEntity;
import com.gqz.equip.entity.TEnWzCrkOrderEntity;
import com.gqz.equip.entity.TEnWzCrkOrderEquipEntity;
import com.gqz.equip.mapper.EnWzJfglMapper;
import com.gqz.equip.mapper.TEnWzCrkOrderEquipMapper;
import com.gqz.equip.service.IEnTjAnalyse;
import com.gqz.equip.service.ITEnInterfaceCodeService;
import com.gqz.equip.vo.*;
import com.gqz.patrol.api.EnPatrolTaskApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 能源-首页统计
 */
@Service
@Slf4j
public class EnTjAnalyseImpl implements IEnTjAnalyse {

    @Resource
    private EquipEnWzJfEquipMapper equipEnWzJfEquipMapper;
    @Resource
    private TEnWzEquipMapper enWzEquipMapper;
    @Resource
    private EnWzKfglMapper wzKfglMapper;
    @Resource
    private EnWzJfglMapper jfglMapper;
    @Resource
    private TEnWzCrkOrderEquipMapper enWzCrkOrderEquipMapper;
    @Resource
    private EnStockTaskMapper enStockTaskMapper;

    @Resource
    private ITEnInterfaceCodeService itEnInterfaceCodeService;

    @Resource
    private EnDefectsSheetApi defectsApi;

    @Resource
    private EnStockEquipDefectApi stockEquipDefectApi;

    @Resource
    private EnPatrolTaskApi patrolTaskApi;

    @Override
    public EnTjAnalyseTzglVo getTzgl() {
        EnTjAnalyseTzglVo resultVo = new EnTjAnalyseTzglVo();
        //获取库房数量
        MPJLambdaWrapper<EnWzKfglEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnWzKfglEntity::getIsDel, "0");
        queryWrapper.select(EnWzKfglEntity::getId);
        Integer kfNum = wzKfglMapper.selectJoinCount(queryWrapper);
        resultVo.setKfNum(kfNum);
        //获取库房设备数量
        MPJLambdaWrapper<TEnWzEquipEntity> kfEquipWrapper = new MPJLambdaWrapper<>();
        kfEquipWrapper.eq(TEnWzEquipEntity::getSfsc, "0");
        kfEquipWrapper.select(TEnWzEquipEntity::getSbbm);
        Integer kfEquipNum = enWzEquipMapper.selectJoinCount(kfEquipWrapper);
        resultVo.setKfEquipNum(kfEquipNum);
        //获取能源站数量
        MPJLambdaWrapper<EnWzJfglEntity> jfWrapper = new MPJLambdaWrapper<>();
        jfWrapper.eq(EnWzJfglEntity::getIsDel, "0");
        jfWrapper.select(EnWzJfglEntity::getId);
        Integer jfNum = jfglMapper.selectJoinCount(jfWrapper);
        resultVo.setNyzNum(jfNum);
        //获取能源站设备数量
        MPJLambdaWrapper<EnWzJfEquipEntity> nyzEquipWrapper = new MPJLambdaWrapper<>();
        nyzEquipWrapper.eq(EnWzJfEquipEntity::getSfsc, "0");
        nyzEquipWrapper.select(EnWzJfEquipEntity::getSbbm);
        Integer nyzEquipNum = equipEnWzJfEquipMapper.selectJoinCount(nyzEquipWrapper);
        resultVo.setNyzEquipNum(nyzEquipNum);

        return resultVo;
    }

    @Override
    public EnTjAnalyseKfglVo getKfgl() {
        EnTjAnalyseKfglVo enTjAnalyseKfglVo = new EnTjAnalyseKfglVo();
        //查询贴签数量和未贴签数量
        MPJLambdaWrapper<TEnWzEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(EnWzEquipBqEntity::getIsTq, "sftq");
        queryWrapper.selectCount(TEnWzEquipEntity::getSblxbm, "tqNum");
        queryWrapper.leftJoin(EnWzEquipBqEntity.class, on -> {
            on.eq(EnWzEquipBqEntity::getSbbm, TEnWzEquipEntity::getSbbm);
            on.eq(EnWzEquipBqEntity::getIsDel, 0);
            return on;
        });
        queryWrapper.groupBy(EnWzEquipBqEntity::getIsTq);
        queryWrapper.eq(TEnWzEquipEntity::getSfsc, 0);
        List<EnTjAnalyseTqVo> tqQkList = enWzEquipMapper.selectJoinList(EnTjAnalyseTqVo.class, queryWrapper);
        //去除tqQkList中的空元素
        tqQkList.removeIf(Objects::isNull);
        tqQkList.forEach(tqEntity -> {
            if (tqEntity.getSftq() != null && "1".equals(tqEntity.getSftq())) {
                enTjAnalyseKfglVo.setTqNum(tqEntity.getTqNum());
            } else {
                enTjAnalyseKfglVo.setWtqNum(tqEntity.getTqNum());
            }
        });
        //获取库房设备数量
        MPJLambdaWrapper<TEnWzEquipEntity> kfEquipZsWrapper = new MPJLambdaWrapper<>();
        kfEquipZsWrapper.eq(TEnWzEquipEntity::getSfsc, "0");
        kfEquipZsWrapper.select(TEnWzEquipEntity::getSbbm);
        Long kfEquipNum = enWzEquipMapper.selectCount(kfEquipZsWrapper);
        //分组查询
        MPJLambdaWrapper<TEnWzEquipEntity> kfEquipWrapper = new MPJLambdaWrapper<>();
        kfEquipWrapper.eq(TEnWzEquipEntity::getSfsc, "0");
        kfEquipWrapper.select(TEnWzEquipEntity::getSblxbm);
        kfEquipWrapper.select(TEnWzEquipEntity::getSblxmc);
        kfEquipWrapper.selectCount(TEnWzEquipEntity::getSblxmc, "kcNum");
        kfEquipWrapper.groupBy(TEnWzEquipEntity::getSblxbm, TEnWzEquipEntity::getSblxmc);
        List<EnTjAnalyseKfEquipVo> equipVoLists = enWzEquipMapper.selectJoinList(EnTjAnalyseKfEquipVo.class, kfEquipWrapper);
        equipVoLists.forEach(equip -> {
            //求占比
            NumberFormat numberFormat = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            numberFormat.setMaximumFractionDigits(2);
            String result = numberFormat.format((float) equip.getKcNum() / (float) kfEquipNum * 100);
            equip.setKcZb(result);
        });
        enTjAnalyseKfglVo.setEquipList(equipVoLists);
        return enTjAnalyseKfglVo;
    }

    @Override
    public EnTjAnalyseCrkVo getCrkList(String type) {
        List<String> week = this.getWeekList();
        //type week:周  month：月
        Calendar calendar = Calendar.getInstance();
        MPJLambdaWrapper<TEnWzCrkOrderEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(TEnWzCrkOrderEntity::getDjrq, "crkTime");
        queryWrapper.select(TEnWzCrkOrderEntity::getType);
        queryWrapper.selectCount(TEnWzCrkOrderEntity::getId, "crkNum");
        queryWrapper.innerJoin(TEnWzEquipEntity.class, TEnWzEquipEntity::getSbbm, TEnWzCrkOrderEquipEntity::getSbbm);
        queryWrapper.innerJoin(TEnWzCrkOrderEntity.class, on -> {
            on.eq(TEnWzCrkOrderEntity::getId, TEnWzCrkOrderEquipEntity::getDjbm);
            on.eq(TEnWzCrkOrderEntity::getSfsc, 0);
            on.eq(TEnWzCrkOrderEntity::getDjshzt, "100");
            return on;
        });
        if ("week".equals(type)) {//周
            // 获取当前日期
            Date eday = calendar.getTime();
            calendar.add(Calendar.DATE, -7);
            Date sday = calendar.getTime();
            queryWrapper.ge(TEnWzCrkOrderEntity::getDjrq, sday);
            queryWrapper.lt(TEnWzCrkOrderEntity::getDjrq, eday);
        } else {//月
            Date eday = calendar.getTime();
            calendar.add(Calendar.MONTH, -1);
            Date sday = calendar.getTime();
            queryWrapper.ge(TEnWzCrkOrderEntity::getDjrq, sday);
            queryWrapper.lt(TEnWzCrkOrderEntity::getDjrq, eday);
        }
        queryWrapper.eq(TEnWzCrkOrderEquipEntity::getSfsc, 0);
        queryWrapper.groupBy(TEnWzCrkOrderEntity::getType);
        queryWrapper.groupBy(TEnWzCrkOrderEntity::getDjrq);
        List<EnTjAnalyseCrkListVo> crkListVosVoLists = enWzCrkOrderEquipMapper.selectJoinList(EnTjAnalyseCrkListVo.class, queryWrapper);
        List<EnTjAnalyseCrkListVo> rkListVosVoLists = new ArrayList<EnTjAnalyseCrkListVo>();
        List<EnTjAnalyseCrkListVo> ckListVosVoLists = new ArrayList<EnTjAnalyseCrkListVo>();
        crkListVosVoLists.forEach(crkListVosVoList -> {
            if ("0".equals(crkListVosVoList.getType())) {
                rkListVosVoLists.add(crkListVosVoList);
            } else {
                ckListVosVoLists.add(crkListVosVoList);
            }
        });
        EnTjAnalyseCrkVo enTjAnalyseCrkVo = new EnTjAnalyseCrkVo();
        try {
            if ("week".equals(type)) {
                SimpleDateFormat smp = new SimpleDateFormat("yyyy-MM-dd");
                for (int j = 0; j < week.size(); j++) {
                    if (ckListVosVoLists.size() > 0) {
                        for (int i = 0; i < ckListVosVoLists.size(); i++) {
                            Date crkTimes = ckListVosVoLists.get(i).getCrkTime();
                            String ckRq = smp.format(crkTimes);
                            if (!week.get(j).equals(ckRq)) {
                                EnTjAnalyseCrkListVo ck = new EnTjAnalyseCrkListVo();
                                ck.setCrkNum(0);
                                ck.setCrkTime(smp.parse(week.get(j)));
                                ck.setType("1");
                                ckListVosVoLists.add(ck);
                            }
                            break;
                        }
                    } else {
                        EnTjAnalyseCrkListVo ck = new EnTjAnalyseCrkListVo();
                        ck.setCrkNum(0);
                        ck.setCrkTime(smp.parse(week.get(j)));
                        ck.setType("1");
                        ckListVosVoLists.add(ck);
                    }

                }
                for (int j = 0; j < week.size(); j++) {
                    if (rkListVosVoLists.size() > 0) {
                        for (int i = 0; i < rkListVosVoLists.size(); i++) {
                            Date crkTime = rkListVosVoLists.get(i).getCrkTime();
                            String djrq = smp.format(crkTime);
                            if (!week.get(j).equals(djrq)) {
                                EnTjAnalyseCrkListVo rk = new EnTjAnalyseCrkListVo();
                                rk.setCrkNum(0);
                                rk.setCrkTime(smp.parse(week.get(j)));
                                rk.setType("0");
                                rkListVosVoLists.add(rk);
                            }
                            break;
                        }
                    } else {
                        EnTjAnalyseCrkListVo rk = new EnTjAnalyseCrkListVo();
                        rk.setCrkNum(0);
                        rk.setCrkTime(smp.parse(week.get(j)));
                        rk.setType("0");
                        ckListVosVoLists.add(rk);
                    }

                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        enTjAnalyseCrkVo.setCkList(ckListVosVoLists);
        enTjAnalyseCrkVo.setRkList(rkListVosVoLists);
        return enTjAnalyseCrkVo;
    }

    @Override
    public List<EnTjAnalyseGdVo> getGdXx(String sdate, String edate) {
        List<EnTjAnalyseGdVo> gdList = new ArrayList<EnTjAnalyseGdVo>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


        //获取盘点任务
        MPJLambdaWrapper<EnStockTaskEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnStockTaskEntity::getIsDel, "0");
        queryWrapper.selectCount(EnStockTaskEntity::getTaskNo,"num");
        queryWrapper.selectAs(EnStockTaskEntity::getTaskStatus,"statusCode");
        //queryWrapper.selectAs(EnStockTaskEntity::getTaskStatusName,"statusName");
        queryWrapper.groupBy(EnStockTaskEntity::getTaskStatus);
        try {
            Date startDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdate);
            Date endDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(edate);
            if(startDateTime != null && !"".equals(startDateTime)){
                queryWrapper.ge(EnStockTaskEntity:: getPlanStime,startDateTime);
            }
            if(endDateTime != null && !"".equals(endDateTime)){
                queryWrapper.le(EnStockTaskEntity:: getPlanStime,endDateTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<EnTjAnalyseGdListVo> pdList = enStockTaskMapper.selectJoinList(EnTjAnalyseGdListVo.class,queryWrapper);
        EnTjAnalyseGdVo pdvo=new EnTjAnalyseGdVo();
        int pdcount=0;
        if(pdList.size()>0){
            for(int i=0;i<pdList.size();i++){
                pdcount+=pdList.get(i).getNum();
                if("1".equals(pdList.get(i).getStatusCode())){
                    pdvo.setPdNum(pdList.get(i).getNum());
                }else if("2".equals(pdList.get(i).getStatusCode())){
                    pdvo.setJdNum(pdList.get(i).getNum());
                }else if("5".equals(pdList.get(i).getStatusCode())){
                    pdvo.setHdNum(pdList.get(i).getNum());
                }else if("99".equals(pdList.get(i).getStatusCode())){
                    pdvo.setGdNum(pdList.get(i).getNum());
                }
            }
        }
        pdvo.setCountNum(pdcount);
        pdvo.setType("0");
        gdList.add(pdvo);
        //获取消缺任务
        var xqList = defectsApi.getGdStatusXx(sdate, edate);
        EnTjAnalyseGdVo xqvo = new EnTjAnalyseGdVo();
        int xqcount=0;
        if(xqList.size()>0){
            for(int i=0;i<xqList.size();i++){
                xqcount+=xqList.get(i).getNum();
                if("10".equals(xqList.get(i).getStatusCode())){
                    xqvo.setPdNum(xqList.get(i).getNum());
                }else if("20".equals(xqList.get(i).getStatusCode())){
                    xqvo.setJdNum(xqList.get(i).getNum());
                }else if("50".equals(xqList.get(i).getStatusCode())){
                    xqvo.setHdNum(xqList.get(i).getNum());
                }else if("100".equals(xqList.get(i).getStatusCode())){
                    xqvo.setGdNum(xqList.get(i).getNum());
                }
            }
        }
        xqvo.setCountNum(xqcount);
        xqvo.setType("2");
        gdList.add(xqvo);
        //获取巡视任务
        var xsList = patrolTaskApi.getGdStatusXx(sdate, edate);
        EnTjAnalyseGdVo xsvo = new EnTjAnalyseGdVo();
        int xscount=0;
        if(xsList.size()>0) {
            for (int i = 0; i < xsList.size(); i++) {
                xscount += xsList.get(i).getNum();
                if ("10".equals(xsList.get(i).getStatusCode())) {
                    xsvo.setPdNum(xsList.get(i).getNum());
                } else if ("15".equals(xsList.get(i).getStatusCode())) {
                    xsvo.setJdNum(xsList.get(i).getNum());
                } else if ("30".equals(xsList.get(i).getStatusCode())) {
                    xsvo.setHdNum(xsList.get(i).getNum());
                } else if ("99".equals(xsList.get(i).getStatusCode())) {
                    xsvo.setGdNum(xsList.get(i).getNum());
                }
            }
        }
        xsvo.setCountNum(xscount);
        xsvo.setType("1");
        gdList.add(xsvo);
        return gdList;
    }

    @Override
    public List<EnTjDefectsListVo> getDefectsStatusXx(String sdate, String edate) {
        List<EnTjDefectsListVo> result = new ArrayList<EnTjDefectsListVo>();
        EnTjDefectsListVo qx = new EnTjDefectsListVo();
        qx.setType("1");
        EnTjDefectsListVo yh=new EnTjDefectsListVo();
        yh.setType("2");
        //获取缺陷隐患
        var defectsList = stockEquipDefectApi.getDefectsStatusXx(sdate, edate);
        int qxcount = 0;
        int yhcount=0;
        for(int i=0;i<defectsList.size();i++){
            if("1".equals(defectsList.get(i).getType())){//缺陷
                if("2".equals(defectsList.get(i).getStatusCode())){
                    qxcount+=defectsList.get(i).getNum();
                    qx.setWclNum(defectsList.get(i).getNum());
                }else if("100".equals(defectsList.get(i).getStatusCode())){
                    qxcount+=defectsList.get(i).getNum();
                    qx.setYclNum(defectsList.get(i).getNum());
                }
            }else{//隐患
                if("2".equals(defectsList.get(i).getStatusCode())){
                    yhcount+=defectsList.get(i).getNum();
                    yh.setWclNum(defectsList.get(i).getNum());
                }else if("100".equals(defectsList.get(i).getStatusCode())){
                    yhcount+=defectsList.get(i).getNum();
                    yh.setYclNum(defectsList.get(i).getNum());
                }
            }
        }
        qx.setZsNum(qxcount);
        yh.setZsNum(yhcount);
        result.add(qx);
        result.add(yh);
        return result;
    }

    /**
     * 获取前一周的年月日
     */
    public List<String> getWeekList() {
        List<String> week = new ArrayList<String>();
        for (int i = 0; i < 7; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String today = format.format(date);
            week.add(today);
        }
        return week;
    }
}
